package com.example.multiactivitydemo

import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.widget.Button
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity

class FourthActivity : AppCompatActivity() {

    // 正确声明所有视图变量
    private lateinit var tvTimer: TextView
    private lateinit var tvLap: TextView
    private lateinit var btnStart: Button
    private lateinit var btnPause: Button
    private lateinit var btnReset: Button

    private var isRunning = false
    private var seconds = 0
    private var lapCount = 0
    private lateinit var handler: Handler
    private lateinit var runnable: Runnable

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_fourth)

        initViews()  // 初始化视图
        setupTimer()

        // 恢复保存的状态
        if (savedInstanceState != null) {
            seconds = savedInstanceState.getInt("seconds", 0)
            isRunning = savedInstanceState.getBoolean("isRunning", false)
            lapCount = savedInstanceState.getInt("lapCount", 0)
            updateTimerDisplay()
            tvLap.text = "圈数: $lapCount"
        }
    }

    private fun initViews() {
        // 正确初始化所有视图变量
        tvTimer = findViewById(R.id.tvTimer)
        tvLap = findViewById(R.id.tvLap)
        btnStart = findViewById(R.id.btnStart)
        btnPause = findViewById(R.id.btnPause)
        btnReset = findViewById(R.id.btnReset)

        btnStart.setOnClickListener { startTimer() }
        btnPause.setOnClickListener { pauseTimer() }
        btnReset.setOnClickListener { resetTimer() }

        // 初始化按钮状态
        btnPause.isEnabled = false
    }

    private fun setupTimer() {
        handler = Handler(Looper.getMainLooper())
        runnable = object : Runnable {
            override fun run() {
                if (isRunning) {
                    seconds++
                    updateTimerDisplay()
                }
                handler.postDelayed(this, 1000)
            }
        }
        handler.postDelayed(runnable, 1000)
    }

    private fun startTimer() {
        isRunning = true
        btnStart.isEnabled = false
        btnPause.isEnabled = true
    }

    private fun pauseTimer() {
        isRunning = false
        btnStart.isEnabled = true
        btnPause.isEnabled = false
        lapCount++
        tvLap.text = "圈数: $lapCount"
    }

    private fun resetTimer() {
        isRunning = false
        seconds = 0
        lapCount = 0
        updateTimerDisplay()
        tvLap.text = "圈数: $lapCount"
        btnStart.isEnabled = true
        btnPause.isEnabled = false
    }

    private fun updateTimerDisplay() {
        val hours = seconds / 3600
        val minutes = (seconds % 3600) / 60
        val secs = seconds % 60
        val time = String.format("%02d:%02d:%02d", hours, minutes, secs)
        tvTimer.text = time
    }

    override fun onSaveInstanceState(outState: Bundle) {
        super.onSaveInstanceState(outState)
        outState.putInt("seconds", seconds)
        outState.putBoolean("isRunning", isRunning)
        outState.putInt("lapCount", lapCount)
    }

    override fun onDestroy() {
        super.onDestroy()
        handler.removeCallbacks(runnable)
    }
}